package com.fancymusic.controller;

import com.fancymusic.entity.*;
import com.fancymusic.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
//@CrossOrigin
public class ReconmandController {

    @Autowired
    IUserService userService;
    @Autowired
    IMusicService musicService;
    @Autowired
    IRecUserSingerService iRecUserSingerService;
    @Autowired
    IRecUserTypeService recUserTypeService;
    @Autowired
    ISingerService iSingerService;

    @RequestMapping("rec/{userId}")
    public BaseResult<Music> rec(@PathVariable("userId") Integer userId) {

        BaseResult<Music> result = new BaseResult<>();
        Map<Integer, Integer> map = new HashMap<>();
        User u = userService.findByUserId(userId);

        List<RecMusic> allMusic = musicService.getAllMusic();
        System.out.println(allMusic + "111111111111111111111111111111111111111111");
        List<RecUserSinger> recUserSingerByuid = iRecUserSingerService.getRecUserSingerByuid(u.getUserId());
        List<RecUserType> recUserTypeByuid = recUserTypeService.getRecUserTypeByuid(u.getUserId());


        Map<Integer, Integer> typeidlist = new HashMap();
        for (RecUserType recUserType : recUserTypeByuid) {
            typeidlist.put(recUserType.getTypeid(), recUserType.getScore());
        }
        System.out.println(typeidlist + "==================typeidlist==================");
        Map<Integer, Integer> singerList = new HashMap();
        for (RecUserSinger recUserSinger : recUserSingerByuid) {
            singerList.put(recUserSinger.getSingerid(), recUserSinger.getScore());
        }

        System.out.println(singerList + "==================singerList==================");

        int i = 0;
        for (RecMusic music : allMusic) {

            if (typeidlist.containsKey(music.getMusicTypeId())) {
                map.put(music.getMusicId(), typeidlist.get(music.getMusicTypeId()));
            }
            System.out.println(++i + "==============1111111111111111111111111111111111");

            if (singerList.containsKey(music.getSingerId())) {


                int o = 0;
                if (typeidlist.containsKey(music.getMusicTypeId())) {
                    o = (int) map.get(music.getMusicId());
                }

                map.put(music.getMusicId(), o + singerList.get(music.getSingerId()));
            }
            System.out.println(map + "-------------------");

        }


        Comparator<Map.Entry<Integer, Integer>> comparator = new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }

        };
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>();

        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            list.add(entry);
        }
        System.out.println(list);
        Collections.sort(list, comparator);

        System.out.println(list);

        List<Map.Entry<Integer, Integer>> list1 = null;
        List<Map.Entry<Integer, Integer>> ll = null;

        if (list.size() < 5) {
            ll = list;

        } else {
            ll = list.subList(0, 5);
        }

        List<Music> list2 = new ArrayList<>();
        for (Map.Entry<Integer, Integer> o : ll) {
            Music musicByMid = musicService.getMusicByMid(o.getKey());
            System.out.println(musicByMid+"===================");
            list2.add(musicByMid);
        }
        System.out.println(ll);
        System.out.println("list2" + list2);
        result.setList(list2);
        return result;
    }
}
